
package de.godlike.autobackup;

import org.apache.log4j.Logger;

import java.io.File;

public class Watchdog implements Runnable {
    private static final int TIME_TO_SLEEP = 5;

    private boolean move = false;

    private Logger logger = Logger.getLogger(this.getClass());

    private final File fileToWatch;

    private void watchHelper() {

        long lastModified = 0;

        while (true) {

            if (lastModified != fileToWatch.lastModified()) {
                lastModified = fileToWatch.lastModified();
                try {
                    Thread.sleep(TIME_TO_SLEEP * 1000);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                continue;
            }
            break;
        }

        if (move) {
            logger.info("Move file: " + fileToWatch.getAbsolutePath());
        } else {
            logger.info("Copy file: " + fileToWatch.getAbsolutePath());
        }

    }

    public Watchdog(File file) {
        this.fileToWatch = file;
        new Thread(this).start();
    }

    @Override
    public void run() {
        watchHelper();
    }
}
